package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.AjmxbEntity;
import cn.com.css.taxInspection.attention.entity.CaseAjxxRelation;
import cn.com.css.taxInspection.attention.mapper.CaseAjxxRelationMapper;
import cn.com.css.taxInspection.attention.service.CaseAjxxRelationService;
import cn.com.css.taxInspection.common.exception.BusinessException;
import cn.com.css.taxInspection.common.exception.code.BaseResponseCode;
import cn.com.css.taxInspection.util.DateUtils;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 专项案件
 */
@Service
@Slf4j
public class CaseAjxxRelationImpl extends ServiceImpl<CaseAjxxRelationMapper, CaseAjxxRelation> implements CaseAjxxRelationService {

    @Resource
    private CaseAjxxRelationMapper caseAjxxRelationMapper;

    @Override
    @DS("oracle")
    // 根据案件编号更新专项案件明细信息
    public int updateZxajmxByAjbh(CaseAjxxRelation caseAjxxRelation) {
        return caseAjxxRelationMapper.updateZxajmxByAjbh(caseAjxxRelation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("oracle")
    public void addCaseAjxxRelation(CaseAjxxRelation vo) {
        vo.setCreateTime(new Date());
        vo.setUpdateTime(null);
        caseAjxxRelationMapper.insert(vo);
        saveBatch(new ArrayList<>());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("oracle")
    public void updateCaseAjxxRelation(CaseAjxxRelation vo) {
        CaseAjxxRelation attentionCase = caseAjxxRelationMapper.selectById(vo.getId());
        if (null == attentionCase) {
            log.error("传入 的 id:{}不合法", vo.getId());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        vo.setUpdateTime(new Date());
        caseAjxxRelationMapper.updateById(vo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("oracle")
    public void deletedCaseAjxxRelation(String id) {
        caseAjxxRelationMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("oracle")
    public void pldeletedCaseAjxxRelation(List<String> idList) {
        caseAjxxRelationMapper.deleteBatchIds(idList);
    }

    @Override
    @DS("oracle")
    public CaseAjxxRelation detailInfo(String id) {
        CaseAjxxRelation attentionCase = caseAjxxRelationMapper.selectById(id);
        if (attentionCase == null) {
            log.error("传入 的 id:{}不合法", id);
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        return attentionCase;
    }

    @Override
    @DS("oracle")
    public List<CaseAjxxRelation> selectAllCaseAjxxRelation() {
        List<CaseAjxxRelation> result = caseAjxxRelationMapper
                .selectList(Wrappers.<CaseAjxxRelation>lambdaQuery().orderByAsc(CaseAjxxRelation::getCreateTime));
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @DS("oracle")
    public void importAjxxCaseRealtionDatas(List<Map<String, Object>> ajxxList, String caseId) {
        // 用于保存批量插入的 CaseAjxxRelation 对象
        List<CaseAjxxRelation> inserts = new ArrayList<>();
        // 循环遍历 ajxxList 中的数据，构建 CaseAjxxRelation 对象
        for (Map<String, Object> map : ajxxList) {
            CaseAjxxRelation cajxx = new CaseAjxxRelation();
            // 设置 caseId
            cajxx.setCaseId(caseId);
            // 获取每个字段并进行必要的转换
            cajxx.setAjbh(getString(map, "jcajbh"));
            cajxx.setNsrmc(getString(map, "nsrmc"));
            cajxx.setNsrsbh(getString(map, "nsrsbh"));
            cajxx.setJcks(getString(map, "jcbm_dm"));
            cajxx.setJcr(getString(map, "jcry"));
            cajxx.setJcajxxuuid(getString(map, "jcajxxuuid"));
            cajxx.setSlry(getString(map, "slry"));
            cajxx.setAjjcztDm(getString(map, "ajjcztdm"));
            cajxx.setAjmc(getString(map, "ajmc"));
            // 日期字段需要格式化
            cajxx.setLarq(DateUtils.formatDate(getString(map, "larq"), "yyyy-MM-dd"));
            cajxx.setZxry(getString(map, "zxry"));
            cajxx.setAjztDm(getString(map, "ajjcztdm"));
            cajxx.setJcksmc(getString(map, "jcksmc"));
            // 将每个构建的对象添加到 inserts 列表中
            inserts.add(cajxx);
        }

        // 批量保存数据
        this.saveBatch(inserts);
    }

    // 提取公共的字符串获取方法，防止 NullPointerException
    private String getString(Map<String, Object> map, String key) {
        Object value = map.get(key);
        return value != null ? value.toString() : "";  // 如果值为 null，返回空字符串
    }


    @Override
    public List<Map<String, Object>> getAjxxByAjbhs(List<String> ajbhs) {
        return caseAjxxRelationMapper.getAjxxListByAjbhs(ajbhs);
    }

    @Override
    public List<AjmxbEntity> ajmxbList(AjmxbEntity req) {
        return caseAjxxRelationMapper.ajmxbList(req);
    }

    @Override
    @DS("oracle")
    public List<AjmxbEntity> handlerAjmxbList(List<AjmxbEntity> ajmxbEntityList) {
        for (AjmxbEntity ajmx : ajmxbEntityList) {
            String jcajxxuuid = ajmx.getJcajxxuuid();
            Map<String, Object> cbRkData = caseAjxxRelationMapper.getCbRkData(jcajxxuuid);
            BigDecimal rksk = (BigDecimal) cbRkData.get("RKSK");
            BigDecimal rkznj = (BigDecimal) cbRkData.get("RKZNJ");
            BigDecimal rkfk = (BigDecimal) cbRkData.get("RKFK");
            ajmx.setRksk(String.valueOf(rksk));
            ajmx.setRkznj(String.valueOf(rkznj));
            ajmx.setRkfk(String.valueOf(rkfk));
            ajmx.setRkxj(String.valueOf(rksk.add(rkznj).add(rkfk)));

            BigDecimal cbsk = (BigDecimal) cbRkData.get("CBSK");
            BigDecimal cbznj = (BigDecimal) cbRkData.get("CBZNJ");
            BigDecimal cbfk = (BigDecimal) cbRkData.get("CBFK");
            ajmx.setCbsk(String.valueOf(cbsk));
            ajmx.setCbznj(String.valueOf(cbznj));
            ajmx.setCbfk(String.valueOf(cbfk));
            ajmx.setCbxj(String.valueOf(cbsk.add(cbznj).add(cbfk)));
            ajmx.setJaqlqszhj(TsBqUtils.getAjhjMcByAjztDm(ajmx.getJaqlqszhj()));
        }

        return ajmxbEntityList;
    }

    @Override
    public long ajmxbListTotal() {
        return caseAjxxRelationMapper.ajmxbListTotal();
    }
}
